package com.chj.es.highlevel;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TestEsHighSdk {

    public static void main(String[] args) throws IOException {
        /*客户端的创建*/
        RestClientBuilder restClientBuilder = RestClient.builder(
                new HttpHost("192.168.0.101",9200,"http"));

        /*高level的客户端*/
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
        String indexName = "high_sdk_test";
        String docId = "1";

        /*创建索引*/
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        /* 这里可以对索引对象进行属性设置 */
//        createIndexRequest.settings(Settings.builder()
//        .put("index.number_of_shards",3)
//        .put("index.codec","best_compression"));
//        Map<String,Object> message = new HashMap<>();
//        message.put("type","text");
//        Map<String,Object> properties = new HashMap<>();
//        properties.put("message",message);
//        Map<String,Object> mapping = new HashMap<>();
//        mapping.put("properties",properties);
//        createIndexRequest.mapping(mapping);
//
//        createIndexRequest.mapping("{" +"\"msg\":\"Java Low Level REST Client\""+"}", XContentType.JSON);
//        XContentBuilder.builder();

        CreateIndexResponse createIndexResponse
                = restHighLevelClient.indices().create(createIndexRequest,RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.index());
        System.out.println(createIndexResponse.isAcknowledged());


        /*索引(保存)文档*/
        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.id(docId);

        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
        xContentBuilder.startObject();
        {
            xContentBuilder.field("user","hankin");
            xContentBuilder.timeField("postData",new Date());
            xContentBuilder.field("message","Go ELK");
        }
        xContentBuilder.endObject();
        indexRequest.source(xContentBuilder);
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(index.getIndex());
        System.out.println(index.getId());


        /*查询文档*/
        GetRequest getRequest = new GetRequest(indexName, docId);
        GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(" get indexname = "+getResponse.getIndex());
        if(getResponse.isExists()){
            System.out.println("获取的文档："+getResponse.getSourceAsString());
        }else{
            System.out.println("文档不存在");
        }

    }

}
